package com.busines.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.busines.Thread.ThreadDemo;
import com.busines.dao.mep.IMepLogicCodeDao;
import com.busines.dao.xunting.ITSchoolDao;
import com.busines.dao.xunting.ITStudentDao;
import com.busines.po.StudentPo;
import com.busines.pojo.xunting.TStudent;
import com.busines.service.IRedisService;
import com.common.util.RedisClient;

@Service("redisService")
public class RedisServiceImpl implements IRedisService{

	@Resource(name="redisClient")
	private RedisClient redisClient;
	
	@Resource(name="tStudentDao")
	private ITStudentDao tStudentDao;
	
	@Resource(name="tSchoolDao")
	private ITSchoolDao tSchoolDao;
	
	@Resource(name="mepLogicCodeDao")
	private IMepLogicCodeDao mepLogicCodeDao;
	
	private static final String STUDENT_CACHER ="student_cacher";
	private static final String STUDENT_CACHER_HASHMAP ="student_map";
	@Override
	public void test1(){
		//redisClient.set("hello123", "hello123", 60);
		try {
			if(redisClient.exists("hello123")){
				System.out.println(redisClient.get("hello123"));
			}else{
				System.out.println("not exists");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		redisClient.set("hello123", "hello123");
		redisClient.setExpire("hello123", 15);
		
		System.out.println("发送成功");
	}

	@Override
	public void setObjectCache() {
		TStudent student = tStudentDao.selectByPrimaryKey(100000001);
		if(redisClient.setObjectCache(STUDENT_CACHER+"_student", student)){
			System.out.println("对象成功放入缓存");
		}
	}

	@Override
	public TStudent getObjectCache() {
		return redisClient.getObjectCache(STUDENT_CACHER+"_student", TStudent.class);
	}

	@Override
	public void setObjectCacheHash(Object obj) {
		//TStudent student = getObjectCache();
		redisClient.setObjectCacheHash(STUDENT_CACHER_HASHMAP, obj, -1);
	}

	@Override
	public TStudent getObjectCacheHash() {
		return redisClient.getObjectCacheHsha(STUDENT_CACHER_HASHMAP, TStudent.class);
	}

	@Override
	public StudentPo getStudentPoCacheByLock() {
		return redisClient.getStudentPoCacheByLock(STUDENT_CACHER_HASHMAP, StudentPo.class);
	}

	@Override
	public void test2() {
		
		for(int i=0;i<100;i++){
			Thread demo = new Thread(new ThreadDemo());
			demo.start();
		}
		
		Thread demo1 = new Thread(new ThreadDemo());
		Thread demo2 = new Thread(new ThreadDemo());
		Thread demo3 = new Thread(new ThreadDemo());
		Thread demo4 = new Thread(new ThreadDemo());
		Thread demo5 = new Thread(new ThreadDemo());
		Thread demo6 = new Thread(new ThreadDemo());
		Thread demo7 = new Thread(new ThreadDemo());
		Thread demo8 = new Thread(new ThreadDemo());
		Thread demo9 = new Thread(new ThreadDemo());
		Thread demo10 = new Thread(new ThreadDemo());
		demo1.start();
		demo2.start();
		demo3.start();
		demo4.start();
		demo5.start();
		demo6.start();
		demo7.start();
		demo8.start();
		demo9.start();
		demo10.start();
	}

	

}
